using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Reflection;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif

namespace DeflateLib.Tests
{
    [TestClass]
    public class EncoderTests
    {
        [TestMethod]
        public void TestZero1()
        {
            MemoryStream ms = new MemoryStream();
            Encoder e = new Encoder(ms);
            e.WriteSyncBlock(false, true);
            Assert.AreEqual(new byte[] { 3, 0 }, ms.ToArray());
        }

        [TestMethod]
        public void TestZero2()
        {
            MemoryStream ms = new MemoryStream();
            Encoder e = new Encoder(ms);
            e.WriteSyncBlock(true, true);
            Assert.AreEqual(new byte[] { 1, 0, 0, 255, 255 }, ms.ToArray());
        }

        [TestMethod]
        public void TestZero1and2()
        {
            MemoryStream ms = new MemoryStream();
            Encoder e = new Encoder(ms);
            e.WriteSyncBlock(false, false);
            e.WriteSyncBlock(true, true);
            Assert.AreEqual(new byte[] { 2, 4, 0, 0, 255, 255 }, ms.ToArray());
        }

        [TestMethod]
        public void TestBlock2()
        {
            MemoryStream ms = new MemoryStream();
            Encoder e = new Encoder(ms);
            e.Encode(new byte[] { 0, 1 }, 0, 2, true);
            //static trees 00110000, 00110001, 0000000
            Assert.AreEqual(new byte[] { 0x63, 0x60, 4, 0 }, ms.ToArray());
        }

        [TestMethod]
        public void TestNoneCompressed()
        {
            MemoryStream ms = new MemoryStream();
            Encoder e = new Encoder(ms);
            List<byte> data = new List<byte>();
            for (int i = 144; i < 255; i++)
            {
                data.Add((byte)i);
            }
            e.Encode(data.ToArray(), 0, data.Count, true);
            byte[] result = ms.ToArray();
            Assert.AreEqual(data.Count + 5, result.Length);
            Assert.AreEqual(1, result[0]);
            Assert.AreEqual(data.Count, result[1]);
            Assert.AreEqual(0, result[2]);
            Assert.AreEqual((byte)(~data.Count), result[3]);
            Assert.AreEqual(255, result[4]);
        }
    }
}
